﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlatformerCS
{
    /// <summary>
    /// Bullet for the weapons.
    /// </summary>
    public class Bullet
    {
        protected float speed;
        protected Vector2 position;
        protected Vector2 direction;
        protected Texture2D texture;
        protected Rectangle rectangle;
        protected int timer;
        protected int lifeTime;
        protected Boolean wasted;

        public enum BulletType
        {
            GunBullet,
            LaserBullet
        };

        /// <summary>
        /// Position of the bullet.
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        /// <summary>
        /// Direction of the bullet.
        /// </summary>
        public Vector2 Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        /// <summary>
        /// The collision rectangle.
        /// </summary>
        public Rectangle Rectangle
        {
            get { return rectangle; }
            set { rectangle = value; }
        }

        /// <summary>
        /// Should the weapon disappear ?
        /// </summary>
        public Boolean Wasted
        {
            get { return wasted; }
            set { wasted = value; }
        }

        public Bullet(BulletType type, ContentManager content, Vector2 position, Vector2 direction)
        {
            this.position = position;
            this.direction = direction;
            wasted = false;
            timer = 0;
            InitializeBullet(content, type);
            UpdateRectangle();
        }

        public void Update()
        {
            // Consume bullet over time
            timer++;

            // Update position and bounding rectangle
            position += direction * speed;
            UpdateRectangle();

            // When bullet goes beyond the upper bound
            // Or when the bullet time has expired
            if (/*position.Y + rectangle.Height > 0 ||*/ timer > lifeTime)
                wasted = true;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);
        }

        /// <summary>
        /// Does the bullet collide with the entity ?
        /// </summary>
        /// <param name="e">The entity the bullet may collide with.</param>
        /// <returns></returns>
        public Boolean Collide(Entity e)
        {
            return rectangle.Intersects(e.Rectangle);
        }

        protected void UpdateRectangle()
        {
            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }

        private void InitializeBullet(ContentManager content, BulletType type)
        {
            switch (type)
            {
                case BulletType.GunBullet:
                    speed = 11f;
                    lifeTime = 120;
                    texture = content.Load<Texture2D>("Weapons/gunBullet");
                    break;
                default:
                    break;

            }
        }
    }
}
